package com.zhida.smartcontrol.launch.bottom;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.jay.tools.base.BaseFragment;
import com.tan.fonticon.widget.IconTextView;
import com.zhida.smartcontrol.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * author: tanjianpei
 * date: 2019/7/11 011 16:08
 * description:
 */
public abstract class BaseBottomFragment extends BaseFragment {

    private final ArrayList<BottomTabBean> TAB_BEANS = new ArrayList<>();
    private final ArrayList<BottomItemFragment> ITME_FRAGMENTS = new ArrayList<>();

    private final LinkedHashMap<BottomTabBean, BottomItemFragment> ITMES = new LinkedHashMap<>();

    // 默认展示的Fragment
    private int mCurrentFragment = 0;
    private int mIndextFragment = 0;
    private int mClickedColor = Color.RED;

    public abstract LinkedHashMap<BottomTabBean, BottomItemFragment> setItems(ItemBuilder builder);

    public abstract int setIndexFragment();

    @ColorInt  // 注解表示必须是一个颜色
    public abstract int setClickedColor();

    @BindView(R.id.ll_bottom)
    LinearLayout mBottomBar;

    @Override
    protected void initPresenter() {

    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_base_bottom;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIndextFragment = setIndexFragment();
        if (setClickedColor() != 0) {
            mClickedColor = setClickedColor();
        }

        final ItemBuilder builder = ItemBuilder.builder();
        final LinkedHashMap<BottomTabBean, BottomItemFragment> items = setItems(builder);

        ITMES.putAll(items);

        for (Map.Entry<BottomTabBean, BottomItemFragment> item : ITMES.entrySet()) {
            final BottomTabBean key = item.getKey();
            final BottomItemFragment value = item.getValue();
            TAB_BEANS.add(key);
            ITME_FRAGMENTS.add(value);
        }
    }

    @Override
    protected void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        final int size = ITMES.size();// 提出size目的是减少循环时ITMES.size()
        for (int i = 0; i < size; i++) {
            final BottomTabBean bean = TAB_BEANS.get(i);

            LayoutInflater.from(getContext()).inflate(R.layout.fragment_base_item, mBottomBar, true);
            final LinearLayout item = (LinearLayout) mBottomBar.getChildAt(i);
            //设置每个ITEM的点击事件
            item.setTag(i);
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final int tag = (int) view.getTag();
                    resetColor();
                    final LinearLayout item = (LinearLayout) view;
                    final ImageView itemIcon = (ImageView) item.getChildAt(0);
                    final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);

                    itemIcon.setImageResource(bean.getSelect_icon());
                    itemTitle.setTextColor(mClickedColor);

                    // 显示的，隐藏的
                    showHideFragment(ITME_FRAGMENTS.get(tag), ITME_FRAGMENTS.get(mCurrentFragment));
                    mCurrentFragment = tag;
                }
            });

            final ImageView itemIcon = (ImageView) item.getChildAt(0);
            final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);

            // 未选中状态
            itemIcon.setImageResource(bean.getNormal_icon());
            itemTitle.setText(bean.getTitle());

            // 当前选中
            if (i == mIndextFragment) {
                itemIcon.setImageResource(bean.getSelect_icon());
                itemTitle.setTextColor(mClickedColor);
            }
        }

        final SupportFragment[] fragmentArray = ITME_FRAGMENTS.toArray(new SupportFragment[size]);
        loadMultipleRootFragment(R.id.frame_main, mIndextFragment, fragmentArray);
    }

    private void resetColor() {
        final int count = mBottomBar.getChildCount();
        for (int i = 0; i < count; i++) {
            final LinearLayout item = (LinearLayout) mBottomBar.getChildAt(i);
            final ImageView itemIcon = (ImageView) item.getChildAt(0);
            final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);

            itemIcon.setImageResource(TAB_BEANS.get(i).getNormal_icon());
            itemTitle.setTextColor(Color.GRAY);
        }

    }

}
